﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XcoAppSpaces.Core;
using XcoAppSpaces.Contracts.Exceptions;
using System.Net;

namespace test.XcoAppSpaces.CF
{
	[TestClass]
    public class testSocketAuthentication
    {
		[TestMethod]
		public void CommunicationWithAuthenticatedSockets()
		{
			using (XcoAppSpace client = new XcoAppSpace("tcp.port=8001;tcp.key=secure123"),
				server = new XcoAppSpace("tcp.port=8002;tcp.key=secure123"))
			{
				server.Run<string>(msg => { });

				//should not throw an exception
				client.Connect<string>(Dns.GetHostName() + ":8002");
			}
		}

		[TestMethod]
		[ExpectedException(typeof(XcoCommunicationException))]
		public void CannotConnectWithInvalidKey()
		{
			using (XcoAppSpace client = new XcoAppSpace("tcp.port=8001;tcp.key=secure123"),
				server = new XcoAppSpace("tcp.port=8002;tcp.key=secure456"))
			{
				server.Run<string>(msg => { });
				client.Connect<string>(Dns.GetHostName() + ":8002");
			}
		}

		[TestMethod]
		[ExpectedException(typeof(XcoCommunicationException))]
		public void CannotConnectWithoutKey()
		{
			using (XcoAppSpace client = new XcoAppSpace("tcp.port=8001"),
				server = new XcoAppSpace("tcp.port=8002;tcp.key=secure123"))
			{
				server.Run<string>(msg => { });
				client.Connect<string>(Dns.GetHostName() + ":8002");
			}
		}
    }
}
